import { MongoClient } from "mongodb";

const url = "mongodb://127.0.0.1:27017/";
const client = new MongoClient(url);

const records = [
  {
    name: "Westbrook",
    age: 32,
  },
  {
    name: "Howard",
    age: 35,
  },
];

async function run() {
  try {
    await client.connect();
    const db = client.db("hello");
    const res = await db.command({ ping: 1 }); // 测试数据库是否成功
    console.log("connected", res);
    // 数据的插入
    const userCollection = db.collection("user");
    // 比较操作符
    // const results = await userCollection.find({ age: { $gt: 30 } }).toArray();
    // console.log(results);

    // 逻辑操作符
    // const results = await userCollection.find({ age: { $lt: 30 }, name: "AD" }).toArray();
    // const condition = {
    //   $or: [{ age: { $gt: 30 } }, { name: "xiaobao" }],
    // };
    // const results = await userCollection.find(condition).toArray();

    // 元素操作符
    // const results = await userCollection.find({ hobby: { $exists: true } }).toArray();
    const results = await userCollection.find({ age: { $type: "number" } }).toArray();
    console.log(results);
  } catch (error) {
    console.error("error", error);
  } finally {
    await client.close();
  }
}

run();
